package k1_jdbc.demo02.personOperation.controller;

import k1_jdbc.demo02.personOperation.pojo.Person;
import k1_jdbc.demo02.personOperation.service.PersonService;
import k1_jdbc.tool.Const;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

public class PersonController {

    PersonService ps = new PersonService();

    /**
     * 测试添加Person
     */
    public void testInsertPerson(){

        //@TODO 这里可以使用 Scanner 从控制台接收数据后封装成Person
        Person person1 = new Person(
                UUID.randomUUID().toString().toUpperCase(),
                "郭德纲",
                "小本",
                LocalDate.of(1977,9,9).format(DateTimeFormatter.ofPattern(Const.DATEFORMATTER)),
                200000
        );
        Person person2 = new Person(
                UUID.randomUUID().toString().toUpperCase(),
                "于谦",
                "幼本",
                LocalDate.of(1968,9,28).format(DateTimeFormatter.ofPattern(Const.DATEFORMATTER)),
                100000
        );
        Person person3 = new Person(
                UUID.randomUUID().toString().toUpperCase(),
                "岳云鹏",
                "胎本",
                LocalDate.of(1986,2,11).format(DateTimeFormatter.ofPattern(Const.DATEFORMATTER)),
                80000
        );

        boolean result = ps.insert(person1,person2,person3);

        if(result){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }

    /**
     * 测试修改 Person
     */
    public void testUpdatePerson(){

        //@TODO 这里可以使用 Scanner 从控制台接收数据后封装成Person
        Person person1 = new Person(
                "88F5A5D8-C457-450A-B50F-D5230D856B6C",
                "郭德纲",
                "本科",
                LocalDate.of(1977,9,9).format(DateTimeFormatter.ofPattern(Const.DATEFORMATTER)),
                300000
        );
        Person person2 = new Person(
                "D70E4D6B-456B-4743-8D93-9E7210983217",
                "于谦",
                "硕士",
                LocalDate.of(1968,9,28).format(DateTimeFormatter.ofPattern(Const.DATEFORMATTER)),
                150000
        );

        boolean result = ps.update(person1,person2);

        if(result){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
    }


    /**
     * 测试删除Person方法
     * 采用软删除的形式，只修改字段，不真正删除数据
     */
    public void testDeletePerson(){
        boolean result = ps.delete("88F5A5D8-C457-450A-B50F-D5230D856B6C","D70E4D6B-456B-4743-8D93-9E7210983217");
        if(result){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
    }

    /**
     * 测试查询所有未删除的Person
     */
    public void testSelectAllPerson(){
        List<Person> personList = ps.selectAllPerson();
        for (Person person : personList) {
            System.out.println(person);
        }
    }

    /**
     * 测试按id查询Person
     */
    public void testSelectPersonById(){
        Person person = ps.selectPersonById("88F5A5D8-C457-450A-B50F-D5230D856B6C");
        System.out.println(person);
    }

    /**
     * 测试分页查询
     */
    public void testSelectPersonForPage(){
        List<Person> personList = ps.selectPersonByPage(3,2);
        for (Person person : personList) {
            System.out.println(person);
        }
    }

    public static void main(String[] args) {
        PersonController pc = new PersonController();
        pc.testInsertPerson();
        pc.testInsertPerson();
//        pc.testUpdatePerson();
//        pc.testDeletePerson();
//        pc.testSelectAllPerson();
//        pc.testSelectPersonById();
        pc.testSelectPersonForPage();
    }
}
